package com.telecom.cms.actions;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.telecom.cms.util.Constant;
import com.telecom.cms.dao.GetConnect;

/**
 * @author yueyingnishang
 * 
 */
public class CollectionDelAction2 extends Action {

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping,
	 *      org.apache.struts.action.ActionForm,
	 *      javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub

		int folderid = Integer.parseInt(request.getParameter("parfolderid"));//文件夹的 id
		int parfolderid=0;//文件夹的父节点id
		int app_id = 0;//查得文件夹id对应的应用id
	
		Connection conn = GetConnect.getCsConnection();
		if (conn!=null){
			conn.setAutoCommit(false);
		}
		//查得删除点父节点的id
		String findParentId="select PAR_FOLEDER_ID from " +
				Constant.SEG_USER_FOLDER_DEF +
				" where FOLEDER_ID="+folderid;
		PreparedStatement ps1=conn.prepareStatement(findParentId);
		ResultSet rs=ps1.executeQuery();
		if(rs.next()){
			parfolderid=rs.getInt("PAR_FOLEDER_ID");
		}
		//删除用户文件夹表中的应用记录
		String delete = "delete from " +
				Constant.SEG_USER_FOLDER_DEF +
				" where FOLEDER_ID="+folderid;
		ps1 = conn.prepareStatement(delete);

		ps1.executeUpdate();

		if (ps1 != null) {

			ps1.close();

		}
		// end
		// 删除应用主题表的对应记录

		// 先取文件夹表的文件ID的对应记录
		PreparedStatement psmt1 = conn
				.prepareStatement(" select APP_SUBJECT_ID from " +
						Constant.SEG_APP_SUBJECT_DEF +
						" where FOLEDER_ID = "
						+folderid);
		ResultSet rs1 = psmt1.executeQuery();

		if (rs1.next()) {

			app_id = rs1.getInt(1);

		}
		if (rs1 != null) {

			rs1.close();
		}
		if (psmt1 != null) {

			psmt1.close();
		}

		String deleteApp = " delete from " +
				Constant.SEG_APP_SUBJECT_DEF +
				" where FOLEDER_ID="+folderid;
		PreparedStatement ps2 = conn.prepareStatement(deleteApp);
		ps2.executeUpdate();
		if (ps2 != null) {

			ps2.close();

		}
		// end
		// 分群规则定义表表删除
		
		String deleteRule="delete from " +
				Constant.SEG_SEG_RULE_DEF +
				" where APP_SUBJECT_ID="+app_id;
		// end
		// 分群规则展示结果定义表插入
		PreparedStatement ps3 = conn.prepareStatement(deleteRule);
		ps3.executeUpdate();
		if (ps3 != null) {

			ps3.close();

		}

		//展示原子表删除
		String deleteShowAtom="delete from " +
				Constant.SEG_SEG_RULE_SHOWATOM +
				" where APP_SUBJECT_ID="+app_id;
		PreparedStatement ps4 = conn.prepareStatement(deleteShowAtom);
		ps4.executeUpdate();
		if (ps4 != null) {
			
			ps4.close();
			
		}

		// end
	
		//将删除点的子节点挂入删除点的父节点
		String changeParent="update " +
				Constant.SEG_USER_FOLDER_DEF +
				" set PAR_FOLEDER_ID ="+parfolderid+"where PAR_FOLEDER_ID="+folderid;
		PreparedStatement ps5 =  conn.prepareStatement(changeParent);
		ps5.executeUpdate();
		if (ps5 != null) {
			
			ps5.close();
		}
		if (conn!=null){
			try{
				conn.commit();
				conn.setAutoCommit(true);
			}catch(Exception e){
				conn.rollback();
				conn.setAutoCommit(true);
				return mapping.findForward("failure");
			}
		}

		return mapping.findForward("success");
	}

}
